package com.haixiaoke.saas.enterprise.house.controller;

import javax.servlet.http.HttpServletResponse;

import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.HouseInspection;
import com.haixiaoke.saas.house.service.IHouseInspectionService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 验房记录Controller
 *
 * @author xiaolong
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/enterprise/house/inspection")
public class EnterpriseHouseInspectionController extends BaseController {
    @Autowired
    private IHouseInspectionService houseInspectionService;

    /**
     *
     * 查询验房记录列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(HouseInspection houseInspection) {
        startPage();
        houseInspection.setTenantId(TenantContext.getTenantId());
        List<HouseInspection> list = houseInspectionService.selectHouseInspectionList(houseInspection);
        return getDataTable(list);
    }

    /**
     * 导出验房记录列表
     */
    @PostMapping("/export")
    @TenantScope
    public void export(HttpServletResponse response, HouseInspection houseInspection) {
        houseInspection.setTenantId(TenantContext.getTenantId());
        List<HouseInspection> list = houseInspectionService.selectHouseInspectionList(houseInspection);
        ExcelUtil<HouseInspection> util = new ExcelUtil<HouseInspection>(HouseInspection.class);
        util.exportExcel(response, list, "验房记录数据");
    }

    /**
     * 获取验房记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:query')")
    @GetMapping(value = "/{houseInspectionId}")
    public AjaxResult getInfo(@PathVariable("houseInspectionId") String houseInspectionId) {
        return success(houseInspectionService.selectHouseInspectionByHouseInspectionId(houseInspectionId));
    }


    /**
     * 获取验房记录设备损坏信息
     */
    @GetMapping(value = "/process/{houseInspectionIds}")
    @TenantScope
    public AjaxResult getProcessInfo(@PathVariable String[] houseInspectionIds) {
        HouseInspection houseInspection = new HouseInspection();
        houseInspection.setHouseInspectionIds(houseInspectionIds);
        houseInspection.setTenantId(TenantContext.getTenantId());
        return success(houseInspectionService.getProcessInfo(houseInspection));
    }


    /**
     * 新增换退验房记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:add')")
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        houseInspection.setOperatorId(userId);
        houseInspection.setTenantId(TenantContext.getTenantId());
        return toAjax(houseInspectionService.insertHouseInspection(houseInspection));
    }

    /**
     * 新增日常验房验房记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:add')")
    @PostMapping("/routineInspection")
    @TenantScope
    @RepeatSubmit
    public AjaxResult routineInspection(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        houseInspection.setOperatorId(userId);
        houseInspection.setTenantId(TenantContext.getTenantId());
        return toAjax(houseInspectionService.routineInspection(houseInspection));
    }

    /**
     * 指定验房人员
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:edit')")
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        houseInspection.setOperatorId(userId);
        return toAjax(houseInspectionService.updateHouseInspection(houseInspection));
    }


    /**
     * 更换验房人员
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:change')")
    @PutMapping("/change")
    @TenantScope
    @RepeatSubmit
    public AjaxResult change(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        houseInspection.setOperatorId(userId);
        return toAjax(houseInspectionService.changeHouseInspection(houseInspection));
    }

    /**
     * 删除验房记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:remove')")
    @DeleteMapping("/{houseInspectionIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable String[] houseInspectionIds) {
        return toAjax(houseInspectionService.deleteHouseInspectionByHouseInspectionIds(houseInspectionIds));
    }

    /**
     * 新增同一人验房打扫
     */
    @PreAuthorize("@ss.hasPermi('enterprise:inspection:add')")
    @PostMapping("/sweepAndInspection")
    @TenantScope
    @RepeatSubmit
    public AjaxResult sweepAndInspection(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        houseInspection.setOperatorId(userId);
        houseInspection.setTenantId(TenantContext.getTenantId());
        return houseInspectionService.insertSweepAndInspection(houseInspection);
    }
}
